package com.example.wheresmystuff2;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This is the Login Activity that allows you to login in or sign up for the application
 * @author Team 26
 */
public class LogIn extends Activity {
	Database db;
	private static String current_User;
	
	/**
	 * Method that is call when created. Creates required instance variables to display a list of items.
	 * @param savedInstanceState the state that was last saved
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_log_in);
  		db = new Database(this);
		
  		//Visuals for the Login Activity
		final EditText userField = (EditText) findViewById(R.id.loginID);
		final EditText passwordField = (EditText) findViewById(R.id.password);
	    Button sign_up_button = (Button) findViewById(R.id.signup);
	    Button sign_in_button = (Button) findViewById(R.id.signin);
	    final TextView errors = (TextView) findViewById(R.id.si_error_field);

	    //Sign up button
	    sign_up_button.setOnClickListener(new OnClickListener(){
	      /**
	       * This onClick method brings you to the Sign Up activity where you can create an account
	       */
	    	public void onClick(View v)
	    	{
	    		Intent myIntent = new Intent(LogIn.this, SignUp.class);
	    		LogIn.this.startActivity(myIntent);
	    	}
	    });
	    
	    // Sign in button
	    sign_in_button.setOnClickListener(new OnClickListener(){
	      /**
	       * This onClick method checks user information and moves to SuccessfulSignIn activity if correct
	       */
	    	public void onClick(View v)
	    	{
	    		db.open();
	    		String user = userField.getText().toString();
	    		String password = passwordField.getText().toString();
	    	  
	    		try{
	    			ArrayList<Object> list = db.getUserRowAsArray(user);
	    			
	    			String e = (String) list.get(1);
	    			if(e.equals(password)){
	    				int locked = (Integer) list.get(4);
	    				if (locked == 0){
	    					//check if default user
	    					if(user.equals("poop"))
	    						Toast.makeText(getApplicationContext(), "Logged in as default user successfully", Toast.LENGTH_SHORT).show();
	    					//check if default admin
	    					else if (user.equals("admin"))
	    						Toast.makeText(getApplicationContext(), "Logged in as default admin successfully", Toast.LENGTH_SHORT).show();
	    					if(!((Integer)list.get(3) == 3))	{  
	    						list.set(3, 3);
	    						db.updateUserRow((String) list.get(0), (String) list.get(1), (String) list.get(2), 3, (Integer) list.get(4), (Integer) list.get(5));
	    					} 
	    					current_User=user;
	    					Intent myIntent = new Intent(LogIn.this, SuccessfulSignIn.class);
	    					LogIn.this.startActivity(myIntent);
	    					Toast.makeText(getApplicationContext(), "Login successfully", Toast.LENGTH_SHORT).show();
	    				}
	    			}
	    			else {
	    				errors.setText("Incorrect Email/Password");
	    				int numAttempts = (Integer)list.get(3);
	    				int locked = 0;
	    				numAttempts--;
	    				if (numAttempts <= 0 || locked == 1){
	    					locked = 1;
	    					numAttempts = 0;
	    					errors.setText("Account locked out");
	    				}
	    			
	    				db.updateUserRow((String) list.get(0), (String) list.get(1), (String) list.get(2), numAttempts, locked, (Integer) list.get(5));
	    			}
	    		}
	    		catch(Exception e) {
	    			errors.setText("User does not exist.");
	    		}
	    		db.close();
	    	}
	    });
	}
	
	/**
	 * Method that creates options menu
	 * @param menu the menu used
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.log_in, menu);
		return true;
	}
	
	public static String getCurrentUser(){
		return current_User;
	}
}
